<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增配置 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:cloud-sync-outlined',
                tooltip: '同步存储结构',
                popConfirm: {
                  title: '是否立即同步',
                  placement: 'left',
                  confirm: handleSyncDataBase.bind(null, record),
                },
              },
              {
                icon: 'ant-design:code-outlined',
                tooltip: '代码生成',
                onClick: codeGenreate.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <generateFromModal @register="registerModal" @success="handleSuccess" />
    <generateCodeGen @register="registerCodeGenModal" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    selectGenerateTable,
    syncDataBase,
    deleteGenerateTable,
  } from '/@/api/generate/generate.api';

  import { useModal } from '/@/components/Modal';
  import generateFromModal from './generateFromModal.vue';
  import generateCodeGen from './generateCodeGen.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { columns, searchFormSchema } from './generateFrom.data';

  export default defineComponent({
    name: 'GenerateManagement',
    components: { BasicTable, generateFromModal, TableAction, generateCodeGen },
    setup() {
      const [registerModal, { openModal }] = useModal();
      const [registerCodeGenModal, { openModal: openGenModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        title: '配置列表',
        api: selectGenerateTable,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }
      function codeGenreate(record: Recordable) {
        openGenModal(true, {
          record,
        });
      }

      function handleDelete(record: Recordable) {
        deleteGenerateTable({ id: record.id }, handleSuccess());
      }
      const { createMessage } = useMessage();
      function handleSyncDataBase(record: Recordable) {
        syncDataBase({ id: record.id }).then((res) => {
          createMessage.success(res);
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        codeGenreate,
        registerCodeGenModal,
        handleSyncDataBase,
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
